<?php
date_default_timezone_set("Asia/Shanghai");
class NewBudget_XmlDb{
	public $doc;
	private $docPath;
	public function __construct($doc){
		$this->docPath=$doc;
		$this->doc=new DOMDocument(); 
		$this->doc->load($doc);
	}
	public function xselect($xqs){
		$xpath=new DOMXpath($this->doc);
		return $xpath->query($xqs);
	}
	public function getData($node,$nName){//js:node.getElementsByTagName('nName')[0].nodeValue
		return $node->getElementsByTagName($nName)->item(0)->firstChild->data;
	}
	public function addNode($xqs,$node){
		$parent=$this->xselect($xqs);
		try{
			$parent->item(0)->insertBefore($node,$parent->item(0)->firstChild);
		}catch(Exception $e){
			return false;
		}
		return true;
	}
	public function saveDoc(){
		$this->doc->save($this->docPath);
	}
	public function changeValue($xqs,$at,$va){
		//$va=ereg_replace("<","&lt;",$va);
		$node=$this->xselect($xqs);
		try{
			$node->item(0)->setAttribute($at,$va);
		}catch(Exception $e){
			return false;
		}
		return true;
	}
}
?>